package com.service.impl;

import com.dao.UserMapper;
import com.entity.User;
import com.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.dao.UserMapper;
import com.entity.User;
import com.exception.ServiceException;
import com.service.UserService;
import com.util.StringUtil;
import com.vo.UserBackVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    //    后台管理员登陆 pj
    public User findUserAdmin(User u){
        String login_name = u.getLogin_name();
        User user=userMapper.findUserAdmin(login_name);
        return user;
    }



    //后台分页查询所有的用户 pj
    public PageInfo findBackUser(String pageNo, UserBackVo userBackVo){

        List<UserBackVo> list = userMapper.findBackUser(userBackVo);
//       把字符串转成int类型
        int pageno= StringUtil.getInt(pageNo);

        PageHelper.startPage(pageno, 2);
        if(null==list){
            throw new ServiceException("没有记录...");
        }else{
            PageInfo<User> info=new PageInfo(list);
            return info;
        }


    }
    //后台修改用户信息 pj
    @Override
    public int modifyBackUser(User user) {

        int i =userMapper.modifyBackUser(user);
        if(0==i){
            throw new ServiceException("修改失败");
        }return i;

    }

    @Override
    public AjaxResult showBackUser(Integer id) {
        AjaxResult result=new AjaxResult(true,"",null);
        User user = userMapper.showBackUser(id);
        System.out.println("================id"+id);
        System.out.println("================user"+user);
        result.setObj(user);
//        if(user!=null){
//            result.setObj(user);
//        }else{
//            throw new ServiceException("查询失败");
//        }
        return result;
    }

    //    杨跃森
    @Override
    public void regist(User user) {
        User user1=userMapper.selectByname(user);
        if (user1==null){
            int i = userMapper.insertUser(user);
            System.out.println(i);
        }else{
            //throw new ServiceException("用户名已经存在");
        }

    }
    //杨跃森
    @Override
    public User findByname(User u) {

        User user = userMapper.selectByname(u);
        if (user==null){
            throw new ServiceException("用户密码错误");
        }
        return user;
    }
    //杨跃森
    @Override
    public Integer findByEmail(String email) {
        Integer integer = userMapper.selectByEmail(email);
        if (integer>0){
            throw new ServiceException("邮箱重复，请重新输入邮箱");
        }

        return integer;

    }

    //杨跃森
    @Override
    public int updateUser(User user) {
        return  userMapper.update(user);
    }
    //杨跃森
    @Override
    public User findById(Integer id) {
        return userMapper.selectById(id);
    }
    //杨跃森
    @Override
    public AjaxResult findUser(String login_name, String password) {
        AjaxResult ajaxResult = new AjaxResult();
        User user = userMapper.selectUser(login_name);
        if (user==null){
            throw new ServiceException("用户名密码错误");
        }else {
            if (password.equals(user.getPassword())){
                ajaxResult.setObj(user);
                ajaxResult.setSuccess(true);
            }
            else {
                ajaxResult.setSuccess(false);
            }
        }


        return ajaxResult;
    }
}